<?php
class ProductsController extends AppController {

	var $name = 'Products';

	function index() {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			$this->Product->recursive = 0;
			pr($this->paginate());
			$this->set('products', $this->paginate());
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function view($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			if (!$id) {
				$this->Session->setFlash(__('Invalid product', true));
				$this->redirect(array('action' => 'index'));
			}
			$this->set('product', $this->Product->read(null, $id));
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function add() {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			if (!empty($this->data)) {
				$this->Product->create();
				if ($this->Product->save($this->data)) {
					$this->Session->setFlash(__('The product has been saved', true));
					$this->redirect(array('action' => 'index'));
				} else {
					$this->Session->setFlash(__('The product could not be saved. Please, try again.', true));
				}
			}
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function edit($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			if (!$id && empty($this->data)) {
				$this->Session->setFlash(__('Invalid product', true));
				$this->redirect(array('action' => 'index'));
			}
			if (!empty($this->data)) {
				if ($this->Product->save($this->data)) {
					$this->Session->setFlash(__('The product has been saved', true));
					$this->redirect(array('action' => 'index'));
				} else {
					$this->Session->setFlash(__('The product could not be saved. Please, try again.', true));
				}
			}
			if (empty($this->data)) {
				$this->data = $this->Product->read(null, $id);
			}
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function delete($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			if (!$id) {
				$this->Session->setFlash(__('Invalid id for product', true));
				$this->redirect(array('action'=>'index'));
			}
			if ($this->Product->delete($id)) {
				$this->Session->setFlash(__('Product deleted', true));
				$this->redirect(array('action'=>'index'));
			}
			$this->Session->setFlash(__('Product was not deleted', true));
			$this->redirect(array('action' => 'index'));
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}
	
	function search(){
		$keyword = $this->data['keyword'];
		$key_genre = $this->data['genre'];
		$key_category = $this->data['product'];
		$key_kriter = $this->data['kriter'];
		$result = null;
		
		$query1_music_rock = "select * from products as Product,producers as Producer,musics as Music where name like'%".$keyword."%' and category='Muzik' and genre='Rock' and Product.id=Music.id and Music.producer_id=Producer.id ";
		$query1_music_pop = "select * from products as Product,producers as Producer,musics as Music where name like'%".$keyword."%' and category='Muzik' and genre='Pop' and Product.id=Music.id and Music.producer_id=Producer.id ";
		$query1_music_rap = "select * from products as Product,producers as Producer,musics as Music where name like'%".$keyword."%' and category='Muzik' and genre='Rap' and Product.id=Music.id and Music.producer_id=Producer.id ";
		$query1_music_klasik = "select * from products as Product,producers as Producer,musics as Music where name like'%".$keyword."%' and category='Muzik' and genre='Klasik' and Product.id=Music.id and Music.producer_id=Producer.id ";
		$query1_music_jazz = "select * from products as Product,producers as Producer,musics as Music where name like'%".$keyword."%' and category='Muzik' and genre='Jazz' and Product.id=Music.id and Music.producer_id=Producer.id ";
		$query1_music_tasavvuf = "select * from products as Product,producers as Producer,musics as Music where name like'%".$keyword."%' and category='Muzik' and genre='Tasavvuf' and Product.id=Music.id and Music.producer_id=Producer.id ";
		$query1_film_polisiye = "select * from products as Product,producers as Producer,films as Film where name like'%".$keyword."%' and category='Film' and genre='Polisiye' and Product.id=Film.id and Film.producer_id=Producer.id ";
		$query1_film_macera = "select * from products as Product,producers as Producer,films as Film where name like'%".$keyword."%' and category='Film' and genre='Macera' and Product.id=Film.id and Film.producer_id=Producer.id ";
		$query1_film_komedi = "select * from products as Product,producers as Producer,films as Film where name like'%".$keyword."%' and category='Film' and genre='Komedi' and Product.id=Film.id and Film.producer_id=Producer.id ";;
		$query1_film_belgesel = "select * from products as Product,producers as Producer,films as Film where name like'%".$keyword."%' and category='Film' and genre='Belgesel' and Product.id=Film.id and Film.producer_id=Producer.id ";
		$query1_film_dram = "select * from products as Product,producers as Producer,films as Film where name like'%".$keyword."%' and category='Film' and genre='Dram' and Product.id=Film.id and Film.producer_id=Producer.id ";
		$query1_film_fantastik = "select * from products as Product,producers as Producer,films as Film where name like'%".$keyword."%' and category='Film' and genre='Fantastik' and Product.id=Film.id and Film.producer_id=Producer.id ";
		$query1_film_tarihi = "select * from products as Product,producers as Producer,films as Film where name like'%".$keyword."%' and category='Film' and genre='Tarihi' and Product.id=Film.id and Film.producer_id=Producer.id ";
		$query1_film_romantik = "select * from products as Product,producers as Producer,films as Film where name like'%".$keyword."%' and category='Film' and genre='Romantik' and Product.id=Film.id and Film.producer_id=Producer.id ";
		$query1_film_korku = "select * from products as Product,producers as Producer,films as Film where name like'%".$keyword."%' and category='Film' and genre='Korku' and Product.id=Film.id and Film.producer_id=Producer.id ";
		$query1_film_gerilim = "select * from products as Product,producers as Producer,films as Film where name like'%".$keyword."%' and category='Film' and genre='Gerilim' and Product.id=Film.id and Film.producer_id=Producer.id ";
		$query1_book_roman = "select * from products as Product,producers as Producer,books as Book where name like'%".$keyword."%' and category='Kitap' and genre='Roman' and Product.id=Book.id and Book.producer_id=Producer.id ";
		$query1_book_siir = "select * from products as Product,producers as Producer,books as Book where name like'%".$keyword."%' and category='Kitap' and genre='Siir' and Product.id=Book.id and Book.producer_id=Producer.id ";
		$query1_book_oyku = "select * from products as Product,producers as Producer,books as Book where name like'%".$keyword."%' and category='Kitap' and genre='Öykü' and Product.id=Book.id and Book.producer_id=Producer.id ";
		$query1_book_felsefe = "select * from products as Product,producers as Producer,books as Book where name like'%".$keyword."%' and category='Kitap' and genre='Felsefe' and Product.id=Book.id and Book.producer_id=Producer.id ";
		$query1_book_inceleme = "select * from products as Product,producers as Producer,books as Book where name like'%".$keyword."%' and category='Kitap' and genre='İnceleme-Araştırma' and Product.id=Book.id and Book.producer_id=Producer.id ";
		$query1_book_bilim = "select * from products as Product,producers as Producer,books as Book where name like'%".$keyword."%' and category='Kitap' and genre='Bilim-Teknik' and Product.id=Book.id and Book.producer_id=Producer.id ";
		$query1_book = "select * from products as Product,producers as Producer,books as Book where name like'%".$keyword."%' and category='Kitap' and Product.id=Book.id and Book.producer_id=Producer.id ";
		$query1_film = "select * from products as Product,producers as Producer,films as Film where name like'%".$keyword."%' and category='Film' and Product.id=Film.id and Film.producer_id=Producer.id ";
		$query1_music = "select * from products as Product,producers as Producer,musics as Music where name like'%".$keyword."%' and category='Muzik' and Product.id=Music.id and Music.producer_id=Producer.id ";
		$query1_all_music = "select * from products as Product,producers as Producer,musics as Music where name like'%".$keyword."%' and Product.id=Music.id and Music.producer_id=Producer.id  ";
		$query1_all_book = "select * from products as Product,producers as Producer,books as Book where name like'%".$keyword."%' and  Product.id=Book.id and Book.producer_id=Producer.id ";
		$query1_all_film = "select * from products as Product,producers as Producer,films as Film where name like'%".$keyword."%' and  Product.id=Film.id and Film.producer_id=Producer.id  ";
		$query1_all_1 = "select category from products where name like'%".$keyword."%' ";
		$query1_all_2 = "select product.category from producer,product where producer.fname like'%".$keyword."%' or producer.lname like'%".$keyword."%'";
		$query2_0 = "select * from products as Product,producers as Producer,books as Book where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%'or Book.publisher like'%".$keyword."%')  and Producer.id=Book.producer_id and Book.id=Product.id and Product.name not like'%".$keyword."%'";
		$query2_0_roman = "select * from products as Product,producers as Producer,books as Book where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%' or Book.publisher like'%".$keyword."%')  and Producer.id=Book.producer_id and Book.id=Product.id and Product.name not like'%".$keyword."%' and Product.genre='Roman' ";
		$query2_0_siir = "select * from products as Product,producers as Producer,books as Book where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%' or Book.publisher like'%".$keyword."%')  and Producer.id=Book.producer_id and Book.id=Product.id and Product.name not like'%".$keyword."%' and Product.genre='Siir' ";
		$query2_0_oyku = "select * from products as Product,producers as Producer,books as Book where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%' or Book.publisher like'%".$keyword."%')  and Producer.id=Book.producer_id and Book.id=Product.id and Product.name not like'%".$keyword."%' and Product.genre='Öykü' ";
		$query2_0_felsefe = "select * from products as Product,producers as Producer,books as Book where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%' or Book.publisher like'%".$keyword."%')  and Producer.id=Book.producer_id and Book.id=Product.id and Product.name not like'%".$keyword."%' and Product.genre='Felsefe' ";
		$query2_0_inceleme = "select * from products as Product,producers as Producer,books as Book where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%' or Book.publisher like'%".$keyword."%')  and Producer.id=Book.producer_id and Book.id=Product.id and Product.name not like'%".$keyword."%' and Product.genre='İnceleme-Araştırma' ";
		$query2_0_bilim = "select * from products as Product,producers as Producer,books as Book where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%' or Book.publisher like'%".$keyword."%')  and Producer.id=Book.producer_id and Book.id=Product.id and Product.name not like'%".$keyword."%' and Product.genre='Bilim-Teknik' ";
		$query2_1 = "select * from products as Product,producers as Producer,films as Film where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%' or Film.stars like'%".$keyword."%')  and Producer.id=Film.producer_id and Film.id=Product.id and Product.name not like'%".$keyword."%'";
		$query2_1_polisiye = "select * from products as Product,producers as Producer,films as Film where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%' or Film.stars like'%".$keyword."%')  and Producer.id=Film.producer_id and Film.id=Product.id and Product.name not like'%".$keyword."%' and Product.genre='Polisiye'";
		$query2_1_macera = "select * from products as Product,producers as Producer,films as Film where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%' or Film.stars like'%".$keyword."%')  and Producer.id=Film.producer_id and Film.id=Product.id and Product.name not like'%".$keyword."%' and Product.genre='Macera'";
		$query2_1_komedi = "select * from products as Product,producers as Producer,films as Film where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%' or Film.stars like'%".$keyword."%')  and Producer.id=Film.producer_id and Film.id=Product.id and Product.name not like'%".$keyword."%'and Product.genre='Komedi'";
		$query2_1_belgesel = "select * from products as Product,producers as Producer,films as Film where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%' or Film.stars like'%".$keyword."%')  and Producer.id=Film.producer_id and Film.id=Product.id and Product.name not like'%".$keyword."%'and Product.genre='Belgesel'";
		$query2_1_dram = "select * from products as Product,producers as Producer,films as Film where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%' or Film.stars like'%".$keyword."%')  and Producer.id=Film.producer_id and Film.id=Product.id and Product.name not like'%".$keyword."%'and Product.genre='Dram'";
		$query2_1_fantastik = "select * from products as Product,producers as Producer,films as Film where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%' or Film.stars like'%".$keyword."%')  and Producer.id=Film.producer_id and Film.id=Product.id and Product.name not like'%".$keyword."%'and Product.genre='Fantastik'";
		$query2_1_tarihi = "select * from products as Product,producers as Producer,films as Film where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%' or Film.stars like'%".$keyword."%')  and Producer.id=Film.producer_id and Film.id=Product.id and Product.name not like'%".$keyword."%'and Product.genre='Tarihi'";
		$query2_1_romantik = "select * from products as Product,producers as Producer,films as Film where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%' or Film.stars like'%".$keyword."%')  and Producer.id=Film.producer_id and Film.id=Product.id and Product.name not like'%".$keyword."%'and Product.genre='Romantik'";
		$query2_1_korku = "select * from products as Product,producers as Producer,films as Film where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%' or Film.stars like'%".$keyword."%')  and Producer.id=Film.producer_id and Film.id=Product.id and Product.name not like'%".$keyword."%'and Product.genre='Korku'";
		$query2_1_gerilim = "select * from products as Product,producers as Producer,films as Film where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%' or Film.stars like'%".$keyword."%')  and Producer.id=Film.producer_id and Film.id=Product.id and Product.name not like'%".$keyword."%'and Product.genre='Gerilim'";
		$query2_2 = "select * from products as Product,producers as Producer,musics as Music where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%')  and Producer.id=Music.producer_id and Music.id=Product.id and Product.name not like'%".$keyword."%'";
		$query2_2_rock = "select * from products as Product,producers as Producer,musics as Music where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%')  and Producer.id=Music.producer_id and Music.id=Product.id and Product.name not like'%".$keyword."%'and Product.genre='Rock'";
		$query2_2_pop = "select * from products as Product,producers as Producer,musics as Music where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%')  and Producer.id=Music.producer_id and Music.id=Product.id and Product.name not like'%".$keyword."%'and Product.genre='Pop'";
		$query2_2_rap = "select * from products as Product,producers as Producer,musics as Music where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%')  and Producer.id=Music.producer_id and Music.id=Product.id and Product.name not like'%".$keyword."%'and Product.genre='Rap'";
		$query2_2_klasik = "select * from products as Product,producers as Producer,musics as Music where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%')  and Producer.id=Music.producer_id and Music.id=Product.id and Product.name not like'%".$keyword."%'and Product.genre='Klasik'";
		$query2_2_jazz = "select * from products as Product,producers as Producer,musics as Music where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%')  and Producer.id=Music.producer_id and Music.id=Product.id and Product.name not like'%".$keyword."%'and Product.genre='Jazz'";
		$query2_2_tasavvuf = "select * from products as Product,producers as Producer,musics as Music where (Producer.fname like'%".$keyword."%' or Producer.lname like'%".$keyword."%')  and Producer.id=Music.producer_id and Music.id=Product.id and Product.name not like'%".$keyword."%'and Product.genre='Tasavvuf'";
	    $query_dil = "select * from products as Product,films as Film,producers as Producer where Product.id=Film.id and Producer.id=Film.producer_id  and Film.language like'%".$keyword."%'";
	if($key_category == "all")
	{
	  $result1 = $this->Product->query($query1_all_book);
	  $result2 = $this->Product->query($query1_all_film);
	  $result3 = $this->Product->query($query1_all_music);
	  $result4 = $this->Product->query($query2_0);
	  $result5 = $this->Product->query($query2_1);
	  $result6 = $this->Product->query($query2_2);
	  $result = array_merge((array)$result1, (array)$result2, (array)$result3, (array)$result4, (array)$result5, (array)$result6);
	}
	
	else if($key_category == "Kitap" && ($key_genre == "all_kitap" || $key_genre == "all"))
	{
		if($key_kriter == "kriter_tumu")
		{
		$result1 = $this->Product->query($query1_book);
		$result2 = $this->Product->query($query2_0);
		$result = array_merge((array)$result1, (array)$result2);
		
		}
		else if($key_kriter == "kriter_ad")
		{
		 $result = $this->Product->query($query1_book);
		
		}
		else if($key_kriter == "kriter_yazar")
		{
		 $result = $this->Product->query($query2_0);
		}
	  
	}
	
	else if($key_category == "Film" && ($key_genre == "all_film" || $key_genre == "all"))
	{
		if($key_kriter == "kriter_tumu")
		{
		$result1 = $this->Product->query($query1_film);
		$result2 = $this->Product->query($query2_1);
		$result = array_merge((array)$result1, (array)$result2);
		
		}
		else if($key_kriter == "kriter_ad")
		{
		 $result = $this->Product->query($query1_film);
		
		}
		else if($key_kriter == "kriter_yonetmen")
		{
		 $result = $this->Product->query($query2_1);
		 
		}
		else if($key_kriter == "kriter_dil")
		{
	     $result = $this->Product->query($query_dil);
	     
		}  
	}
	
	else if($key_category == "Müzik" && ($key_genre == "all_muzik" || $key_genre == "all"))
	{
		if($key_kriter == "kriter_tumu")
		{
		$result1 = $this->Product->query($query1_music);
		$result2 = $this->Product->query($query2_2);
		$result = array_merge((array)$result1, (array)$result2);
		}
		else if($key_kriter == "kriter_ad")
		{
		 $result = $this->Product->query($query1_music);
		}
		else if($key_kriter == "kriter_sarkici")
		{
		 $result = $this->Product->query($query2_2);
		}
	}
	
	else if($key_category == "Kitap" && $key_genre == "roman")
	{
		$result1 = $this->Product->query($query1_book_roman);
		$result2 = $this->Product->query($query2_0_roman);
		$result = array_merge((array)$result1, (array)$result2);
	}
	
	else if($key_category == "Kitap" && $key_genre == "siir")
	{
		$result1 = $this->Product->query($query1_book_siir);
		$result2 = $this->Product->query($query2_0_siir);
		$result = array_merge((array)$result1, (array)$result2);
	}
	
	else if($key_category == "Kitap" && $key_genre == "oyku")
	{
		$result1 = $this->Product->query($query1_book_oyku);
		$result2 = $this->Product->query($query2_0_oyku);
		$result = array_merge((array)$result1, (array)$result2);
	}
	
	else if($key_category == "Kitap" && $key_genre == "felsefe")
	{
		$result1 = $this->Product->query($query1_book_felsefe);
		$result2 = $this->Product->query($query2_0_felsefe);
		$result = array_merge((array)$result1, (array)$result2);
	}
	
	else if($key_category == "Kitap" && $key_genre == "inceleme_arastirma")
	{
		$result1 = $this->Product->query($query1_book_inceleme);
		$result2 = $this->Product->query($query2_0_inceleme);
		$result = array_merge((array)$result1, (array)$result2);
	}
	
	else if($key_category == "Kitap" && $key_genre == "bilim_teknik")
	{
		$result1 = $this->Product->query($query1_book_bilim);
		$result2 = $this->Product->query($query2_0_bilim);
		$result = array_merge((array)$result1, (array)$result2);
	}
	
	else if($key_category == "Film" && $key_genre == "polisiye")
	{
		$result1 = $this->Product->query($query1_film_polisiye);
		$result2 = $this->Product->query($query2_1_polisiye);
		$result = array_merge((array)$result1, (array)$result2);
	}
	
	else if($key_category == "Film" && $key_genre == "macera")
	{
		$result1 = $this->Product->query($query1_film_macera);
		$result2 = $this->Product->query($query2_1_macera);
		$result = array_merge((array)$result1, (array)$result2);
	}
	
	else if($key_category == "Film" && $key_genre == "komedi")
	{
		$result1 = $this->Product->query($query1_film_komedi);
		$result2 = $this->Product->query($query2_1_komedi);
		$result = array_merge((array)$result1, (array)$result2);
	}
	
	else if($key_category == "Film" && $key_genre == "belgesel")
	{
		$result1 = $this->Product->query($query1_film_belgesel);
		$result2 = $this->Product->query($query2_1_belgesel);
		$result = array_merge((array)$result1, (array)$result2);
	}
	
	else if($key_category == "Film" && $key_genre == "dram")
	{
		$result1 = $this->Product->query($query1_film_dram);
		$result2 = $this->Product->query($query2_1_dram);
		$result = array_merge((array)$result1, (array)$result2);
	}
	
	else if($key_category == "Film" && $key_genre == "fantastik")
	{
		$result1 = $this->Product->query($query1_film_fantastik);
		$result2 = $this->Product->query($query2_1_fantastik);
		$result = array_merge((array)$result1, (array)$result2);
	}
	
	else if($key_category == "Film" && $key_genre == "tarihi")
	{
		$result1 = $this->Product->query($query1_film_tarihi);
		$result2 = $this->Product->query($query2_1_tarihi);
		$result = array_merge((array)$result1, (array)$result2);
	}
	
	else if($key_category == "Film" && $key_genre == "romantik")
	{
		$result1 = $this->Product->query($query1_film_romantik);
		$result2 = $this->Product->query($query2_1_romantik);
		$result = array_merge((array)$result1, (array)$result2);
	}
	
	else if($key_category == "Film" && $key_genre == "korku")
	{
		$result1 = $this->Product->query($query1_film_korku);
		$result2 = $this->Product->query($query2_1_korku);
		$result = array_merge((array)$result1, (array)$result2);
	}
	
	else if($key_category == "Film" && $key_genre == "gerilim")
	{
		$result1 = $this->Product->query($query1_film_gerilim);
		$result2 = $this->Product->query($query2_1_gerilim);
		$result = array_merge((array)$result1, (array)$result2);
	}
	
	else if($key_category == "Müzik" && $key_genre == "rock")
	{
		$result1 = $this->Product->query($query1_music_rock);
		$result2 = $this->Product->query($query2_2_rock);
		$result = array_merge((array)$result1, (array)$result2);
	}
	
	else if($key_category == "Müzik" && $key_genre == "pop")
	{
		$result1 = $this->Product->query($query1_music_pop);
		$result2 = $this->Product->query($query2_2_pop);
		$result = array_merge((array)$result1, (array)$result2);
	}
	
	else if($key_category == "Müzik" && $key_genre == "rap")
	{
		$result1 = $this->Product->query($query1_music_rap);
		$result2 = $this->Product->query($query2_2_rap);
		$result = array_merge((array)$result1, (array)$result2);
	}
	
	else if($key_category == "Müzik" && $key_genre == "klasik")
	{
		$result1 = $this->Product->query($query1_music_klasik);
		$result2 = $this->Product->query($query2_2_klasik);
		$result = array_merge((array)$result1, (array)$result2);
	}
	
	else if($key_category == "Müzik" && $key_genre == "jazz")
	{
		$result1 = $this->Product->query($query1_music_jazz);
		$result2 = $this->Product->query($query2_2_jazz);
		$result = array_merge((array)$result1, (array)$result2);
	}
	
	else if($key_category == "Müzik" && $key_genre == "tasavvuf")
	{
		$result1 = $this->Product->query($query1_music_tasavvuf);
		$result2 = $this->Product->query($query2_2_tasavvuf);
		$result = array_merge((array)$result1, (array)$result2);
	}	
	$this->set("result",$result);
	}	
    
    function cart(){
    	if (!empty($this->data)) {
    		$this->autoRender = false;
    		$cart = $this->Session->read('cart');
    		if($this->data['Product']['operation']=='empty'){
    			$cart = array('products'=>array(),'sum'=>0);
    			$this->Session->write('cart',$cart);
    			return (json_encode($cart));
    		}
    		
    		$id = $this->data['Product']['id'];
    		
    		if($this->data['Product']['operation']=='add'){
	    		if(!$cart){
	    			$cart = array('products'=>array(),'sum'=>0);
	    		}else{
	    			foreach ($cart['products'] as &$item){
	    				if($item['Product']['id']==$id){
	    					$item['Product']['qty']++;
	    					$item['Product']['allprice'] = $item['Product']['qty'] * $item['Product']['price'];
	    					$this->calcSum($cart);
	    					$this->Session->write('cart',$cart);
	    					return (json_encode($cart));
	    				}
	    			}
	    		}
	    		$this->Product->contain(); 		
	    		$product = $this->Product->read(array('id','name','image','category','price'),$id); 	
	    		$product['Product']['qty'] = 1;
	    		$product['Product']['allprice'] = $product['Product']['price']*1;
				array_push($cart['products'], $product);
				$this->calcSum($cart);
				$this->Session->write('cart',$cart);
				return (json_encode($cart));
    		}elseif($this->data['Product']['operation']=='remove'){
    			for ($i = 0; $i < sizeof($cart['products']); $i++){
    				if($cart['products'][$i]['Product']['id']==$id){
    					unset($cart['products'][$i]);
    					$cart['products'] = array_values($cart['products']);
    					$this->calcSum($cart);
    					$this->Session->write('cart',$cart);
    					return (json_encode($cart));
    				}
    			}
    		}
		}
    }
    
    function calcSum(&$cart){
    	$cart['sum'] = 0;
    	foreach ($cart['products'] as $item){
    		$cart['sum'] += $item['Product']['allprice'];
    	}
    }
    
	function mycart(){
		$this->layout = 'cartlayout';
	}
	
	function pageflip($id = null){
		if($id){
			$this->layout = 'pageflip';
			$this->Product->id = $id;
			$pages = $this->Product->field('pages');
			$pages = split(';', $pages);
			$this->set('pages',$pages);
		} else {
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}
	
	function beforeFilter() {
	    parent::beforeFilter();
	    $this->Auth->allow(array('*'));
    }
}
